#include <cstdint>
#include <iostream>
#include <istream>

using ll = int64_t;
const ll maxn = 1e3+5;
bool map[maxn][maxn],vis[maxn][maxn];
ll T,n,m,k,x0,y0,d0;
const ll dis[4][2] = {
    {0,1},{1,0},
    {0,-1},{-1,0}
};

inline static void solve(){
    std::cin>>n>>m>>k
        >>x0>>y0>>d0;
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=m;j++){
            char c;
            std::cin>>c;
            map[i][j]= c=='.';
            vis[i][j]=false;
        }
    }
    ll ans=1;
    vis[x0][y0]=true;
    while(k--){
        ll nx = x0+dis[d0][0],ny=y0+dis[d0][1];
        if(nx<1 || nx>n || ny<1 || ny>m || !map[nx][ny]){
            d0=(d0+1)%4;
            continue;
        }
        x0=nx,y0=ny;
        if(!vis[nx][ny]){
            ans++;
            vis[nx][ny]=true;
        }
    }
    std::cout<<ans<<"\n";
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>T;
    while(T--){
        solve();
    }
}